package com.gemcoder.system.service;
import static com.gemcoder.system.datasource.JdbcTemplateUtil.executeSQL;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Service;
import static com.gemcoder.system.service.UserSdkService.sdkOfGetCurrentUserName;
import static com.gemcoder.system.service.UserSdkService.sdkOfGetCurrentUserId;
import com.gemcoder.system.bo.Result;
import static com.gemcoder.system.service.permission.PermissionSdkService.sdkOfgetAllPermissions;
import static com.gemcoder.system.service.permission.PermissionSdkService.sdkOfgetAllMenus;
import java.sql.SQLException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.sql.SqlExecutor;

@Service
public class AlarmlistService {

    public Result getAlarmData(Map<String,Object> paramMap){
    // 获取参数
    String projectName = paramMap.get("projectName") != null ? String.valueOf(paramMap.get("projectName")) : null;
    String alarmType = paramMap.get("alarmType") != null ? String.valueOf(paramMap.get("alarmType")) : null;
    String timeRange = paramMap.get("timeRange") != null ? String.valueOf(paramMap.get("timeRange")) : null;
    Integer current = paramMap.get("current") != null ? Integer.valueOf(String.valueOf(paramMap.get("current"))) : 1;
    Integer pageSize = paramMap.get("pageSize") != null ? Integer.valueOf(String.valueOf(paramMap.get("pageSize"))) : 10;

    // 解析时间范围
    String[] timeRangeArray = timeRange != null ? timeRange.split(",") : null;
    String startTime = timeRangeArray != null && timeRangeArray.length > 0 ? timeRangeArray[0] : null;
    String endTime = timeRangeArray != null && timeRangeArray.length > 1 ? timeRangeArray[1] : null;

    // 构建查询SQL
    StringBuilder sql = new StringBuilder("SELECT id, serial_number, project_name, alarm_type, device_name, alarm_content,DATE_FORMAT(report_date, '%Y-%m-%d %H:%i:%s') AS report_date, current_status FROM alarm_list WHERE 1=1");
    List<Object> params = new ArrayList<>();

        if (projectName != null && !projectName.isEmpty()) {
            sql.append(" AND project_name = ?");
            params.add(projectName);
        }
    if (alarmType != null && !alarmType.isEmpty()) {
        sql.append(" AND alarm_type = ?");
        params.add(alarmType);
    }
    if (startTime != null && endTime != null) {
        sql.append(" AND report_date BETWEEN ? AND ?");
        params.add(startTime);
        params.add(endTime);
    }

    // 分页查询
    sql.append(" LIMIT ?, ?");
    params.add((current - 1) * pageSize);
    params.add(pageSize);

    // 执行查询
    List<Map<String, Object>> data = executeSQL(sql.toString(), params.toArray());

    // 查询总条数
    StringBuilder countSql = new StringBuilder("SELECT COUNT(*) as total FROM alarm_list WHERE 1=1");
    List<Object> countParams = new ArrayList<>();
    if (projectName != null) {
        countSql.append(" AND project_name = ?");
        countParams.add(projectName);
    }
    if (alarmType != null) {
        countSql.append(" AND alarm_type = ?");
        countParams.add(alarmType);
    }
    if (startTime != null && endTime != null) {
        countSql.append(" AND report_date BETWEEN ? AND ?");
        countParams.add(startTime);
        countParams.add(endTime);
    }
    List<Map<String, Object>> countResult = executeSQL(countSql.toString(), countParams.toArray());
    int total = countResult.get(0).get("total") != null ? Integer.valueOf(String.valueOf(countResult.get(0).get("total"))) : 0;

    // 返回结果
    Result result = new Result();
    result.setFlag(true);
    result.setMessage("查询成功");
    Map<String, Object> resultData = new HashMap<>();
    resultData.put("total", total);
    resultData.put("data", data);
    result.setData(resultData);
    return result;
}

    public Result exportAlarmData(Map<String,Object> paramMap){
        // 获取参数
        String projectName = paramMap.get("projectName") != null ? String.valueOf(paramMap.get("projectName")) : null;
        String alarmType = paramMap.get("alarmType") != null ? String.valueOf(paramMap.get("alarmType")) : null;
        String timeRange = paramMap.get("timeRange") != null ? String.valueOf(paramMap.get("timeRange")) : null;
        // 解析时间范围
        String[] timeRangeArray = timeRange != null ? timeRange.split(",") : null;
        String startTime = timeRangeArray != null && timeRangeArray.length > 0 ? timeRangeArray[0] : null;
        String endTime = timeRangeArray != null && timeRangeArray.length > 1 ? timeRangeArray[1] : null;

        // 构建查询SQL
        StringBuilder sql = new StringBuilder("SELECT id, serial_number, project_name, alarm_type, device_name, alarm_content,DATE_FORMAT(report_date, '%Y-%m-%d %H:%i:%s') AS report_date, current_status FROM alarm_list WHERE 1=1");
        List<Object> params = new ArrayList<>();

        if (projectName != null && !projectName.isEmpty()) {
            sql.append(" AND project_name = ?");
            params.add(projectName);
        }
        if (alarmType != null && !alarmType.isEmpty()) {
            sql.append(" AND alarm_type = ?");
            params.add(alarmType);
        }
        if (startTime != null && endTime != null) {
            sql.append(" AND report_date BETWEEN ? AND ?");
            params.add(startTime);
            params.add(endTime);
        }


        // 执行查询
        List<Map<String, Object>> data = executeSQL(sql.toString(), params.toArray());

        // 查询总条数
        StringBuilder countSql = new StringBuilder("SELECT COUNT(*) as total FROM alarm_list WHERE 1=1");
        List<Object> countParams = new ArrayList<>();
        if (projectName != null) {
            countSql.append(" AND project_name = ?");
            countParams.add(projectName);
        }
        if (alarmType != null) {
            countSql.append(" AND alarm_type = ?");
            countParams.add(alarmType);
        }
        if (startTime != null && endTime != null) {
            countSql.append(" AND report_date BETWEEN ? AND ?");
            countParams.add(startTime);
            countParams.add(endTime);
        }
        List<Map<String, Object>> countResult = executeSQL(countSql.toString(), countParams.toArray());
        int total = countResult.get(0).get("total") != null ? Integer.valueOf(String.valueOf(countResult.get(0).get("total"))) : 0;

        // 返回结果
        Result result = new Result();
        result.setFlag(true);
        result.setMessage("查询成功");
        Map<String, Object> resultData = new HashMap<>();
        resultData.put("total", total);
        resultData.put("data", data);
        result.setData(resultData);
        return result;
    }

public Result getAllAlarmData(Map<String,Object> paramMap){
    // 构建查询SQL
    StringBuilder sql = new StringBuilder("SELECT id, serial_number, project_name, alarm_type, device_name, alarm_content, report_date, current_status FROM alarm_list");
    List<Map<String, Object>> data = executeSQL(sql.toString());

    // 查询总条数
    StringBuilder countSql = new StringBuilder("SELECT COUNT(*) as total FROM alarm_list");
    List<Map<String, Object>> countResult = executeSQL(countSql.toString());
    int total = countResult.get(0).get("total") != null ? Integer.valueOf(String.valueOf(countResult.get(0).get("total"))) : 0;

    // 返回结果
    Result result = new Result();
    result.setFlag(true);
    result.setMessage("查询成功");
    Map<String, Object> resultData = new HashMap<>();
    resultData.put("total", total);
    resultData.put("data", data);
    result.setData(resultData);
    return result;
}

public Result getAlarmDataByPage(Map<String,Object> paramMap){
    // 获取参数
    Integer current = paramMap.get("current") != null ? Integer.valueOf(String.valueOf(paramMap.get("current"))) : 1;
    Integer pageSize = paramMap.get("pageSize") != null ? Integer.valueOf(String.valueOf(paramMap.get("pageSize"))) : 10;

    // 构建查询SQL
    StringBuilder sql = new StringBuilder("SELECT id, serial_number, project_name, alarm_type, device_name, alarm_content, report_date, current_status FROM alarm_list LIMIT ?, ?");
    List<Object> params = new ArrayList<>();
    params.add((current - 1) * pageSize);
    params.add(pageSize);

    // 执行查询
    List<Map<String, Object>> data = executeSQL(sql.toString(), params.toArray());

    // 查询总条数
    StringBuilder countSql = new StringBuilder("SELECT COUNT(*) as total FROM alarm_list");
    List<Map<String, Object>> countResult = executeSQL(countSql.toString());
    int total = countResult.get(0).get("total") != null ? Integer.valueOf(String.valueOf(countResult.get(0).get("total"))) : 0;

    // 返回结果
    Result result = new Result();
    result.setFlag(true);
    result.setMessage("查询成功");
    Map<String, Object> resultData = new HashMap<>();
    resultData.put("total", total);
    resultData.put("data", data);
    result.setData(resultData);
    return result;
}

    public Result updateAlarmStatus(Map<String, Object> paramMap) {
        Integer id = paramMap.get("id") != null ? Integer.valueOf(String.valueOf(paramMap.get("id"))) : null;
        if (id == null) {
            Result result = new Result();
            result.setFlag(false);
            result.setMessage("更新的告警id不能为空");
            return result;
        }
        String status = paramMap.get("status") != null ? String.valueOf(paramMap.get("status")) : null;
        StringBuilder sql = new StringBuilder("UPDATE  alarm_list set current_status =? WHERE id =?");
        List<Object> params = new ArrayList<>();
        params.add(status);
        params.add(id);
        int updateRows = executeSQL(sql.toString(), params.toArray());
        return new Result(updateRows > 0, updateRows > 0 ? "更新成功" : "更新失败", null);
    }
}